package com.han.jdbc.mybatis._04;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.han.jdbc.mybatis._04.mappers.EmpMapper;
import com.han.jdbc.mybatis.bean.Emp;

/**
* @author hanpeng
* @version 创建时间：2020年7月19日 下午4:37:07
* @ClassName 类名称
* @Description 类描述
*/
public class DynamicSqlTest {
	@Test
	public void testGetEmpByEId() {
		String resource="04mybatis-config.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
			Emp emp = mapper.getEmpByEId(1);
			System.out.println(emp);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}
	}
	@Test
	public void testGetEmpsByConditionIf() {
		String resource="04mybatis-config.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
			Emp emp = new Emp();
			emp.seteId(1);
			
			List<Emp> list = mapper.getEmpsByConditionIf(emp);
			System.out.println(list);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}
	}
	@SuppressWarnings("null")
	@Test
	public void updateEmp() {
		String resource="04mybatis-config.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
			Emp emp = new Emp();
			emp.seteId(1);
			emp.seteName("张三dsfssd");
			boolean result = mapper.updateEmp(emp);
			sqlSession.commit();
			System.out.println(result);
		} catch (IOException e) {
			e.printStackTrace();
			sqlSession.rollback();
		} finally {
			sqlSession.close();
		}
	}
	
	@SuppressWarnings("null")
	@Test
	public void getEmpByEIds() {
		String resource="04mybatis-config.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
			
			Set<Integer> set=new HashSet<Integer>();
			set.add(1);
			set.add(2);
			set.add(3);
			List<Emp> list = mapper.getEmpByEIds(set);
			System.out.println(list);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}
	}
	
	
	public static void main(String[] args) {
		String resource="04mybatis-config.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
			Emp emp = new Emp();
			emp.seteId(1);
			emp.seteName("张三dsd");
			boolean result = mapper.updateEmp(emp);
			sqlSession.commit();
		} catch (IOException e) {
			e.printStackTrace();
			sqlSession.rollback();
		} finally {
			sqlSession.close();
		}
	}
	@Test
	public void testUpdate() throws Exception { 
		String resource="04mybatis-config.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			//SysSimcardMapper mapper = sqlSession.getMapper(SysSimcardMapper.class);
			//SysSimcard simcard = mapper.selectByPrimaryKey(1);
			//System.out.println(simcard);
		} catch (IOException e) {
			e.printStackTrace();
			sqlSession.rollback();
		} finally {
			sqlSession.close();
		}
	}
}
